﻿using CardMonitor.Model;
using CardMonitor.Pages;
using CardMonitor.Utils;
using DotNetSpeech;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CardMonitor.Windows
{
    /// <summary>
    /// CardOptionWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CardOptionWindow : Window
    {
        CardInfo objIDCardInfo = null;
        MainWindow mainWindow = null;
        SpVoice speech = new SpVoice();
        Window currentWindow=null;
        string[] CardInfo = new string[30];

        public CardOptionWindow(MainWindow mainWin)
        {
            InitializeComponent();
            mainWindow= mainWin;
            currentWindow = this;
        }

     /*   private void doCard(object sender, RoutedEventArgs e)
        {
            *//*BillModel model = new BillModel();
            model.SaleType = "2";
            Card card = new Card(model, mainWindow);
            NavigationService.GetNavigationService(this).Navigate(card);
            return;*/
            /*
            BillModel billModel = new BillModel();
            billModel.BillNo = "11";
            billModel.BusiType = "buy";
            billModel.PhoneNumber = "13562261766";
            billModel.CarNo = "鲁E12345";
            Card card = new Card(billModel, mainWindow);
            card.KeyDown += card.MMPage_KeyDown;

            NavigationService.GetNavigationService(this).Navigate(card, UriKind.Relative);
            card.Focus();
            *//*
            //return;
        
            string cardDevice = (string)App.getConfig().IDCardDevice;
            if (!"1".Equals(cardDevice))
            {
                goMakeCard((string)App.getConfig().IDCardCode);
                return;
            }

            string sMsg = string.Empty;
            String sPath = System.Environment.CurrentDirectory + "\\";

            try
            {
                ////读身份证对象
                //objIDCardInfo = IDCardReader.ReadCardInfo(0, out sMsg, sPath);
                //if (!string.IsNullOrEmpty(sMsg))
                //{
                //    showMessage(sMsg);
                //}
                //读身份证对象
                //objIDCardInfo = IDCardReader.ReadCardInfo(0, out sMsg, sPath);
                StringBuilder IdCardInfo = new StringBuilder(409600);
                int ret = CommonInterface.SdtReadCard(0, 1, IdCardInfo, 0);
                if (ret != 0)
                {
                    MessageBox.Show("读取证件信息失败，错误代码：" + ret.ToString());
                    return;
                }

                CardInfo = IdCardInfo.ToString().Split(':');
            }
            catch (Exception ex)
            {
                showMessage("身份证读取失败");
            }

            if (CardInfo != null)
            {
                System.IO.MemoryStream objMSPhoto = new System.IO.MemoryStream(objIDCardInfo.ArrPhotoByte, 0, objIDCardInfo.ArrPhotoByte.Length);

                ImageBrush imageBrush = new ImageBrush();
                ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
                imageBrush.ImageSource = (ImageSource)imageSourceConverter.ConvertFrom(objMSPhoto);

                //byte[] Photo = Convert.FromBase64String(CardInfo[14]);
                //MemoryStream ms = new MemoryStream(Photo);
                //System.Drawing.Image image = System.Drawing.Image.FromStream(ms);

                goMakeCard(CardInfo[9], imageBrush);

                goMakeCard(objIDCardInfo.CardNo, imageBrush);
            }
        }

        private void goMakeCard(String cardCode, ImageBrush image = null)
        {
            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;

            JObject paramJSON = new JObject();
            paramJSON.Add("cardCode", cardCode);
            paramJSON.Add("operation", "forMakeCard");
            httpPostWindow.setParamObject(paramJSON);

            httpPostWindow.setOperation(HttpPostWindow.OperationType.getBillByCard.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "未查询到单据";
                }
                speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);

                showMessage(message);

                return;
            }
           // mainWindow.Speak("请核对信息，确认正常请点击制卡按钮。制卡完成后，请拿走身份证IC卡小票单据");

            //将卡片移动至读卡区域
            //App.getGlobalVariables().cardDeviceComm.SendHexData("F2 00 00 03 43 32 31 03 B2");
            CardDeviceUtil.MoveCardToReadPosition();

            JObject resultObject = httpPostWindow.getResultObject();
            BillModel billModel = JsonConvert.DeserializeObject<BillModel>(resultObject.ToString());


            Card card = new Card(billModel, mainWindow);
            if (null != image)
            {
                card.setPersonImage(image);
            }
            NavigationService navigationService = NavigationService.GetNavigationService(this); 
            navigationService.Navigate(card);
            //NavigationService.GetNavigationService(this).Navigate(card);

        }*/
        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }

        private void CloseWin(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
